#pragma once
#include<iostream>
#include<list>
#include<forward_list>
using namespace std;

#define LSIZE 100000

#include<cstdlib>  //qsort bsearch
#include<stdio.h>  //snprintf
#include<ctime>

void tTest_List(){
	list<string> l;
	char buf[10];

	time_t startTime = clock();
	for (int i = 0; i < LSIZE; i++) {
		try {
			snprintf(buf, 10, "%d", rand());
			l.push_back(buf);	//在末端插入元素
		}
		catch (exception& p) {
			cout << "i= " << i << " " << p.what() << endl;
			abort();
		}
	}
	cout << "list 分配所需时间: " << clock() - startTime << endl;
	cout << "list 大小: " << l.size() << endl;
	cout << "list 最大大小" << l.max_size() << endl;
	cout << "list 起点元素" << l.front() << endl;
	cout << "list 终点元素" << l.back() << endl;

	string target;
	cin >> target;

	startTime = clock();
	list<string>::iterator pItem = ::find(l.begin(), l.end(), target);
	cout << "find 所需时间: " << clock() - startTime << endl;
	if (pItem != l.end())
		cout << "找到元素: " << *pItem << endl;
	else
		cout << "找不到" << endl;

	//调用容器中的排序 sort
	startTime = clock();
	l.sort();
	cout << "list::sort 所需时间: " << clock() - startTime << endl;
}


void tTest_ForwardList() {
	forward_list<string> fl;
	char buf[10];

	time_t startTime = clock();
	for (int i = 0; i < LSIZE; i++) {
		try {
			snprintf(buf, 10, "%d", rand());
			//无 push_back, 只能 push_front
			//在后端插入效率较低 (判断null?)
			fl.push_front(buf);
		}
		catch (exception& p) {
			cout << "i= " << i << " " << p.what() << endl;
			abort();
		}
	}
	//无 back, 无 size
	cout << "forward_list 分配所需时间: " << clock() - startTime << endl;
	cout << "forward_list 最大大小" << fl.max_size() << endl;
	cout << "forward_list 最前(末)端元素" << fl.front() << endl;
	
	string target;
	cin >> target;

	startTime = clock();
	forward_list<string>::iterator pItem = ::find(fl.begin(), fl.end(), target);
	cout << "find 所需时间: " << clock() - startTime << endl;
	if (pItem != fl.end())
		cout << "找到元素: " << *pItem << endl;
	else
		cout << "找不到" << endl;

	//forward_list 容器自身排序 sort
	startTime = clock();
	fl.sort();
	cout << "forward_list::sort 所需时间: " << clock() - startTime << endl;
}
